package menu.sys

import com.google.gson.*
import org.apache.commons.io.FileUtils
import org.junit.Test
import java.io.File

/**
 * @author futao
 * Created on 2017/12/6 - 18:55.
 */
class Test2 {
    @Test
    fun testCase001() {

        val currentRoleType = 1
        val menuJsonObj = JsonParser().parse(FileUtils.readFileToString(File("C:\\Users\\11851\\Desktop\\menu.json"))).asJsonObject

        val oldRoleJsonObj = JsonParser().parse(FileUtils.readFileToString(File("C:\\Users\\11851\\Desktop\\role.json"))).asJsonObject

        val newRoleJsonObj = JsonObject()

        menuJsonObj.entrySet().forEach f1@ { (k, v) ->
            val menu = v.asJsonObject.getAsJsonObject("menu")


            //region menu
            val role_menu_all = JsonObject()
            val role_menu = JsonObject()
            role_menu_all.add("menu", role_menu)
            /*nav=true*/
            if (menu.get("nav").asBoolean) {
                /*有roleType*/
                if (menu.has("roleType")) {
                    /*roleType=currentRoleType*/
                    if (menu.get("roleType").asInt == currentRoleType) {
                        role_menu.apply {
                            add("name", menu.get("name"))
                            add("nav", menu.get("nav"))
                            add("title", menu.get("title"))
                            add("roleType", menu.get("roleType"))
                            menu.get("line")?.let { add("line", it) }
                            menu.get("style")?.let { add("style", it) }
                            menu.get("icon")?.let { add("icon", it) }
                        }
                    }
                    /*roleType!=currentRoleType*/
                    else {
                        return@f1
                    }
                }
                /*不包含roleType*/
                else {
                    role_menu.apply {
                        add("name", menu.get("name"))
                        add("nav", menu.get("nav"))
                        add("title", menu.get("title"))
                        menu.get("line")?.let { add("line", it) }
                        menu.get("style")?.let { add("style", it) }
                        menu.get("icon")?.let { add("icon", it) }
                    }
                }
            }
            /*nav=false*/
            else {
                return@f1
            }
            //endregion


            //region menuMap
            val menuMap = v.asJsonObject.getAsJsonObject("menuMap")


            //region menuMap_old

            val role_menuMap = JsonObject()
            menuMap.entrySet().forEach f2@ { (menuMap_k, menuMap_v) ->
                val role_menuMap_content = JsonObject()
                //region hide = true
                if (menuMap_v.asJsonObject.get("hide").asBoolean) {
                    return@f2
                }
                //endregion

                //region hide = false
                else {
                    //region 有roleType
                    if (menuMap_v.asJsonObject.has("roleType")) {
                        //region currentRoleType = roleTye
                        if (menuMap_v.asJsonObject.get("roleType").asInt == currentRoleType) {
                            role_menuMap.add(menuMap_k, role_menuMap_content)
                            role_menuMap_content.apply {
                                add("id", menuMap_v.asJsonObject.get("id"))
                                add("title", menuMap_v.asJsonObject.get("title"))
                                add("hide", menuMap_v.asJsonObject.get("hide"))
                                add("roleType", menuMap_v.asJsonObject.get("roleType"))
                                menuMap_v.asJsonObject.get("line")?.let { add("line", it) }
                                menuMap_v.asJsonObject.get("style")?.let { add("style", it) }
                                menuMap_v.asJsonObject.get("icon")?.let { add("icon", it) }

                                //region items
                                if (menuMap_v.asJsonObject.has("items")) {
                                    val new_items = JsonArray()
                                    /*items*/
                                    menuMap_v.asJsonObject.getAsJsonArray("items").forEach item@ { item ->
                                        val new_item = JsonObject()
                                        /*item = hide*/
                                        if ((item as JsonObject).get("hide").asBoolean) {
                                            return@item
                                        }
                                        /*item != hide*/
                                        else {
                                            /*has roleType*/
                                            if (item.has("roleType")) {
                                                if (item.get("roleType").asInt == currentRoleType) {
                                                    new_items.add(new_item)
                                                    new_item.apply {
                                                        add("id", item.get("id"))
                                                        add("title", item.get("title"))
                                                        add("roleType", item.get("roleType"))
                                                        item.get("line")?.let { add("line", it) }
                                                        item.get("style")?.let { add("style", it) }
                                                        item.get("icon")?.let { add("icon", it) }
                                                        /*有 ctrl*/
                                                        if (item.asJsonObject.has("ctrl")) {
                                                            //val rightArray = JsonParser().parse(sysControllerListService.getRightCache(menuMap_v.asJsonObject.get("ctrl").asString).toString()).asJsonObject.get("items").asJsonArray
                                                            val new_auth_array = JsonArray()
                                                            val old_items_array = oldRoleJsonObj.getAsJsonObject(k).getAsJsonObject("menuMap").getAsJsonObject(menuMap_k).getAsJsonArray("items")
                                                            val rightArray4ThisCtrl = JsonParser().parse(json).asJsonObject.get("items").asJsonArray
                                                            rightArray4ThisCtrl.forEach { right ->
                                                                old_items_array.forEach { old_item ->
                                                                    if ((old_item as JsonObject).get("id") == item.get("id")) {
                                                                        val old_auth = old_item.get("auth")

                                                                    } else {

                                                                    }
                                                                }
                                                            }

                                                        }
                                                        /*无ctrl*/
                                                        else {
                                                            /*不处理*/
                                                        }
                                                    }
                                                } else {
                                                    return@item
                                                }
                                            }
                                            /*has not roleType */
                                            else {
                                                new_items.add(new_item)
                                                new_item.apply {
                                                    add("id", item.get("id"))
                                                    add("title", item.get("title"))
                                                    item.get("line")?.let { add("line", it) }
                                                    item.get("style")?.let { add("style", it) }
                                                    item.get("icon")?.let { add("icon", it) }
                                                }
                                                if (item.has("ctrl")) {
                                                    //val rightArray = JsonParser().parse(sysControllerListService.getRightCache(menuMap_v.asJsonObject.get("ctrl").asString).toString()).asJsonObject.get("items").asJsonArray
                                                    val rightArray = JsonArray()
                                                    oldRoleJsonObj.getAsJsonObject(k).getAsJsonObject("menuMap").getAsJsonObject(menuMap_k).getAsJsonArray("items")
                                                }
                                            }
                                        }
                                        if (new_items.count() > 0) {
                                            add("items", new_items)
                                        }

                                    }
                                }
                                //endregion

                            }
                            /*有ctrl*/
                            if (menuMap_v.asJsonObject.has("ctrl")) {
//                                val rightArray = JsonParser().parse(sysControllerListService.getRightCache(menuMap_v.asJsonObject.get("ctrl").asString).toString()).asJsonObject.get("items").asJsonArray
                                val new_auth_array = JsonArray()
                                val rightArray4ThisCtrl = JsonParser().parse(json).asJsonObject.get("items").asJsonArray
                                val old_auth_array = oldRoleJsonObj.getAsJsonObject(k).getAsJsonObject("menuMap").getAsJsonObject(menuMap_k).getAsJsonArray("auth")
                                if (old_auth_array.count() > 0) {
                                    rightArray4ThisCtrl.forEach { it1 ->
                                        val new_auth = JsonObject()
                                        new_auth_array.add(new_auth)
                                        val auth_name = it1.asJsonObject.get("name")
                                        val old_auth = old_auth_array.find { it2 -> it2.asJsonObject.get("key") == auth_name }
                                        if (old_auth != null) {
                                            new_auth.apply {
                                                add("key", it1.asJsonObject.get("name"))
                                                add("title", it1.asJsonObject.get("viewName"))
                                                add("val", old_auth.asJsonObject.get("val"))
                                            }
                                        } else {
                                            new_auth.apply {
                                                add("key", it1.asJsonObject.get("name"))
                                                add("title", it1.asJsonObject.get("viewName"))
                                                add("val", Gson().toJsonTree(false))
                                            }
                                        }
                                    }
                                } else {
                                    val new_auth = JsonObject()
                                    new_auth_array.add(new_auth)
                                    rightArray4ThisCtrl.forEach { it1 ->
                                        val auth_name = it1.asJsonObject.get("name")
                                        new_auth.apply {
                                            add("key", it1.asJsonObject.get("name"))
                                            add("title", it1.asJsonObject.get("viewName"))
                                            add("val", Gson().toJsonTree(false))
                                        }
                                    }
                                }
                                role_menuMap_content.add("auth", new_auth_array)
                            }
                            /*无ctrl*/
                            else {
                                /*不处理*/
                            }
                            /*items*/
//                            if ()
                        }
                        //endregion
                        //region currentRoleType != roleType
                        else {
                            return@f2
                        }
                        //endregion
                    }
                    //endregion
                    //region 无roleType
                    else {
                        role_menuMap.add(menuMap_k, role_menuMap_content)
                        role_menuMap_content.apply {
                            add("id", menuMap_v.asJsonObject.get("id"))
                            add("title", menuMap_v.asJsonObject.get("title"))
                            add("hide", menuMap_v.asJsonObject.get("hide"))
                            menuMap_v.asJsonObject.get("line")?.let { add("line", it) }
                            menuMap_v.asJsonObject.get("style")?.let { add("style", it) }
                            menuMap_v.asJsonObject.get("icon")?.let { add("icon", it) }
                        }
                        //region 有ctrl
                        if (menuMap_v.asJsonObject.has("ctrl")) {

                        }
                        //endregion
                        //region 无ctrl
                        else {
                            /*不处理*/
                        }
                        //endregion
                    }
                    //endregion
                }
                //endregion
            }


            //endregion


            //endregion

            /*角色新权限菜单添加role_menu_all */
            role_menu_all.add("menuMap", role_menuMap)
            /*角色新权限菜单添加nav*/
            newRoleJsonObj.add(k, role_menu_all)
        }
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(newRoleJsonObj))
        FileUtils.writeStringToFile(File("C:\\Users\\11851\\Desktop\\newRole.json"), Gson().toJson(newRoleJsonObj), "utf-8")
    }

    val json = "{\n" +
            "      \"viewName\": \"\",\n" +
            "      \"name\": \"com.wise.wises.controller.client.ClientActivityController\",\n" +
            "      \"items\": [\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:getData\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:time\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:consume\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:postData\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:getActivity\"\n" +
            "        }\n" +
            "      ]\n" +
            "    }"
}